import javax.swing.plaf.PanelUI;
import java.util.Stack;

public class TestSingleList {

    //列表节点
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }


    public ListNode head;//表示存储当前链表的头结点的引用

    /**
     * 通过穷举的方式 创建一个链表出来
     * 现在这样做  只是为了能够让初学者对这个结构更好的了解
     * 后期 会改回来
     */
    public void createList(){
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(12);
        ListNode node3 = new ListNode(23);
        ListNode node4 = new ListNode(34);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    /**
     * 打印顺序表
     * 因为链表可以进行查找 但是现在我的头结点为null 查找不了 所以我们定义一个临时变量 cur 不影响我的头结点
     */
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
//        while(this.head != null){
//            System.out.println(this.head.val+" ");
//            this.head = this.head.next;
//        }
        System.out.println();
    }

    /**
     * 从指定节点开始打印链表
     * @param newHead
     */
    public void display(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
    }
    //头插法：O(1)
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法：O(n)
    public void addLast(int data){

        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }else{
            //这样会报空指针异常 所以我们要处理一下
            ListNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }
    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new IndexNotLegalException("Index位置不合法！");
        }
    }
    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data){
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        //说明不是头插也不是尾插 且index合法
        ListNode node = new ListNode(data);
        ListNode cur = findIndexSubOfOne(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode findIndexSubOfOne(int index){
        ListNode cur = head;
        while(index-1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //还有情况没处理
        if(head.val == key){
            head = head.next;
            return;
        }

        ListNode cur = searchPrevOfKey(key);
        if(cur == null)return;
        ListNode del = cur.next;
        //cur.next = cur.next.next;
        cur.next = del.next;
    }

    /**
     * 找到关键字key的前驱
     * @param key
     * @return
     */
    private ListNode searchPrevOfKey(int key){
        ListNode cur = this.head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null)return;
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空单链表
    public void clear(){
        //this.head = null;
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }
    //例1：反转链表
    public ListNode reverseList(){
        //说明链表是空的
        if(head == null){
            return null;
        }
        //说明链表只有1个节点
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;//当前需要翻转的节点
        head.next = null;//head其实就是当前需要翻转的节点的前驱
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    //例2：找到中间节点
    public ListNode middleNode(ListNode head){
        if(head == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    //例3：找倒数第几个节点
    public ListNode findKthToTail(int k){
        if(k <= 0 || head == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(k-1 != 0){
            fast = fast.next;
            if(fast == null){
                return null;//k太大了
            }
            k--;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //例4：链表分割
    public ListNode partition(ListNode pHead, int x){
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                //插入到第一个段
                if(bs == null){
                    //这是第一次插入元素
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                //插入到第二个段
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null){
            return as;
        }
        be.next = as;
        if(as != null){
            ae.next = null;
        }
        return bs;
    }
    //例5：链表的回文结构
    public boolean chkPalindrome(ListNode A){
        if(A == null || A.next == null){
            return true;
        }
        ListNode fast = A;
        ListNode slow = A;
        //找中点
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //翻转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //对比
        while(A != slow){
            if(A.val != slow.val){
                return false;
            }
            //偶数情况下
            if(A.next != slow){
                return true;
            }
            A = A.next;
            slow = slow.next;
        }
        return true;
    }
    //例6：相交链表
//    public static TestSingleList.ListNode getIntersectionNode(TestSingleList.ListNode headA, TestSingleList.ListNode headB){
//        //1.分别求两个链表长度
//        int lenA = 0;
//        int lenB = 0;
//        ListNode pl = headA;//pl 代表永远指向长的链表
//        ListNode ps = headB;//ps 代表永远指向短的链表
//        while(headA != null){
//            lenA++;
//            headA = headA.next;
//        }
//        while(headB != null){
//            lenB++;
//            headB = headB.next;
//        }
//        pl = headA;
//        ps = headB;
//        int len = lenA - lenB;
//        if(len < 0){
//            pl = headB;
//            ps = headA;
//            len = lenB - lenA;
//        }
//        //1.pl 代表永远指向长的链表  2.ps 代表永远指向短的链表  3.len 一定是一个正数
//        while(len != 0){
//            pl = pl.next;
//            len--;
//        }
//        //4.pl走了差值步
//        while(pl != ps && pl != null){
//            pl = pl.next;
//            ps = ps.next;
//        }
//        if(pl == null){
//            return null;
//        }
//        return pl;
//    }
    //例7：判断是否环形链表
//    public boolean hasCycle1(){
//        ListNode fast = head;
//        ListNode slow = head;
//        while(fast != null && fast.next != null){
//            fast = fast.next.next;
//            slow = slow.next;
//            if(fast == slow){
//                return true;
//            }
//        }
//        return false;
//    }
    public boolean hasCycle(){
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return false;
        }
        return true;
    }


    public ListNode detectCycle(){
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        slow = head;
        while(slow != fast){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    /**
     * 递归实现 单链表的逆序打印
     * @param head
     */
    public void printList2(ListNode head){
        if(head == null){
            return;
        }
        if(head.next == null){
            System.out.print(head.val+" ");
            return;
        }
        printList2(head.next);
        System.out.print(head.val+" ");
    }
    /**
     * 非递归实现
     */
    public void printList(){
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null){
            stack.push(cur);
            cur = cur.next;
        }
        while(!stack.empty()){
            ListNode top  = stack.pop();
            System.out.print(top.val+" ");
        }
        System.out.println();
    }

}
